#include "projectile.h"
#include "particle.h"
#include "scene.h"
#define BALA 1
#define LASER 2
#define PLASMA 3

Projectile::Projectile(void)
{
	alive = false;
	tipo = -1;
	posicion = Vector3();
	origen = Vector3();
	velocidad = Vector3();
	owner = -1;
	birth = 0;
	ttl = 0;
	size = Vector2();
	damage = 0;
}

Projectile::~Projectile(void)
{
}

Projectile::Projectile(Vector3 _origen, Vector3 _velocidad, int _owner, int _tipo)
{
	tipo = _tipo;
	origen = _origen;
	velocidad = _velocidad;
	owner = _owner;

	birth = getTime();
	
	switch (_tipo){
		case BALA: //Bala
			ttl = 1000;
			size.x = 0.1;
			size.y = 5;
			damage = 1;
			break;
		case LASER: //Laser
			ttl = 0;
			size.x = 0.4;
			size.y = 2000;
			damage = 30;
			break;
		case PLASMA: //Plasma
			ttl = 3000;
			size.x = 20;
			size.y = 2;
			damage = 10;
			break;
		default:
			ttl = 1000;
	}
	posicion = _origen;
	alive = true;
}

void Projectile::Set(Vector3 _origen, Vector3 _velocidad, int _owner, int _tipo){
	tipo = _tipo;
	origen = _origen;
	velocidad = _velocidad;
	owner = _owner;
	birth = getTime();

	switch (_tipo){
		case BALA: //Bala
			ttl = 1000;
			size.x = 20;
			size.y = 1;
			damage = 1;
			break;
		case LASER: //Laser
			ttl = 0;
			size.x = 0.4;
			size.y = 2000;
			damage = 30;
			break;
		case PLASMA: //Plasma
			ttl = 3000;
			size.x = 40;
			size.y = 1;
			damage = 10;
			break;
		default:
			ttl = 1000;
	}
	posicion = _origen;
	alive = true;
}

void Projectile::recalc(){
	double elapsed = getTime()-birth;
	posicion = origen + velocidad*(elapsed);
	if((elapsed) > ttl) alive = false;
}


void Projectile::render(){

	bool draw = true;
	double elapsed = getTime()-birth;
	Scene * s = Scene::getScene();
	switch (tipo) {
		case 1: //Bala
			glColor4f(1.0,1.0,1.0,1.0);
			if (!s->ActivateTexture("gauss"))
				std::cout<<"Projectile: error load tex1"<<std::endl;
			break;
		case 2: //Laser
			if (!s->ActivateTexture("gauss_noise"))
				std::cout<<"Projectile: error load tex2"<<std::endl;
			glColor4f(1.0,0,0,1.0);
			break;
		case 3: //Plasma
			if (!s->ActivateTexture("gauss2"))
				std::cout<<"Projectile: error load tex3"<<std::endl;
			glColor4f(0,0.2,0.8,1.0);
			break;
		default: 
			glColor4f(1.0,0.2,0.3,1.0);
			break;
	}
	
	if (draw) {
		Scene * s = Scene::getScene();
		glPushAttrib(GL_ALL_ATTRIB_BITS);
		glEnable(GL_DEPTH_TEST);
		glDepthMask(false);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE);
		glEnable(GL_TEXTURE_2D);
		Particle * ammo = new Particle(posicion, velocidad, birth, ttl, size);
		glBegin(GL_QUADS);
			ammo->render();
		glEnd();
		glPopAttrib();
	}
}